﻿
using A.M.ETO;
using A.M.Storage.Application;
using DotNetCore.CAP;
using L.M.Review.Application.IServices;
using L.M.Review.Domain.DTO;
using L.M.Review.Domain.Entity;
using L.M.Review.Infrastruecure.IRepository;
using L.M.Review.Infrastruecure.Repository;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace L.M.Review.Application.Services
{
    public class Process_recordsServices : BaseServices<process_records,string> , IProcess_recordsServices
    {
        private readonly IProcess_recordsRepository process_RecordsRepository;
        private readonly IProcess_nodesServices process_NodesServices;
        private readonly ICapPublisher capPublisher;
        private readonly IProcess_instanceServices process_InstanceServices;
        private readonly ISnfor_approval_formServices snfor_Approval_FormServices;
        public Process_recordsServices(IProcess_recordsRepository process_RecordsRepository, IProcess_instanceServices process_InstanceServices, IProcess_nodesServices process_NodesServices = null, ICapPublisher capPublisher = null, ISnfor_approval_formServices snfor_Approval_FormServices = null) : base(process_RecordsRepository)
        {
            this.process_RecordsRepository = process_RecordsRepository;
            this.process_InstanceServices = process_InstanceServices;
            this.process_NodesServices = process_NodesServices;
            this.capPublisher = capPublisher;
            this.snfor_Approval_FormServices = snfor_Approval_FormServices;
        }

        public IProcess_instanceServices Process_InstanceServices => process_InstanceServices;



        /// <summary>
        /// 添加审批流转状态
        /// </summary>
        /// <param name="process_RecorfUpdInforDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> UpdInforProInfor(Process_RecorfUpdInforDTO process_RecorfUpdInforDTO)
        {

            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                process_RecordsRepository.BeginTranInfor();
                //找到流程实例表
                process_instance? proc_ioleinfor = await process_InstanceServices.GetShowObjAsync(c => c.process_instance_number == process_RecorfUpdInforDTO.process_instance_number);
                if(proc_ioleinfor == null)
                {
                    apiReturn.Code = 500;
                    apiReturn.Message = "审批失败";
                    return apiReturn;
                }
                //添加审批流转记录表
                process_records process_Records = new process_records();
                process_Records.process_records_number = YitIdHelper.NextId().ToString();
                process_Records.application_form_number = process_RecorfUpdInforDTO.approval_form_number;
                process_Records.process_instance_number = process_RecorfUpdInforDTO.process_instance_number;
                process_Records.current_node_number = proc_ioleinfor.current_node_number;
                process_Records.current_node_name = proc_ioleinfor.current_node_name;
                process_Records.operator_number = process_RecorfUpdInforDTO.operator_number;
                process_Records.operator_name = process_RecorfUpdInforDTO.operator_name;
                process_Records.isWart = process_RecorfUpdInforDTO.isWart;
                process_Records.isStyinfor = process_RecorfUpdInforDTO.isStyinfor;
                process_Records.isMyauthId = process_RecorfUpdInforDTO.approval_form_number;
                //判断还有没有下个节点
                if (proc_ioleinfor.to_da_role_number == "")
                { 
                    process_Records.isQuerInfor = true;
                }
                else
                {
                    process_Records.isQuerInfor = false;
                }
                await process_RecordsRepository.AddInfor_Async(process_Records);

                //找到下一个流程节点
                var liuc = await process_NodesServices.GetShowObjAsync(c => c.process_nodes_role_number == proc_ioleinfor.operator_number);
                var gesss = await process_NodesServices.GetShowObjAsync(c => c.upper_node == liuc.lower_node);
                if (gesss.lower_node == "")
                {
                    //获取审批记录的单号
                    await process_InstanceServices.UpdInfor_Async(proc_ioleinfor);
                    var res = await snfor_Approval_FormServices.GetShowObjAsync(c => c.approval_project_number == proc_ioleinfor.XmInfor_id);
                    if (process_RecorfUpdInforDTO.approval_form_number == "1")
                    {
                        string res1 = res.approval_project_number;
                        //执行添加的MQ
                        await capPublisher.PublishAsync("GetScName", res1);
                    }
                    if (process_RecorfUpdInforDTO.approval_form_number == "2")
                    {
                        string res1 = res.approval_project_number;
                        //执行添加的MQ
                        await capPublisher.PublishAsync("GetBomName", res1);
                    }
                    if (process_RecorfUpdInforDTO.approval_form_number == "3")
                    {
                        string res1 = res.approval_project_number;
                        //执行添加的MQ
                        await capPublisher.PublishAsync("GetXsName", res1);
                    }
                    if (process_RecorfUpdInforDTO.approval_form_number == "4")
                    {
                        string res1 = res.approval_project_number;
                        //执行添加的MQ
                        await capPublisher.PublishAsync("GetKcName", res1);
                    }
                    var shjenpiId = await process_InstanceServices.GetShowObjAsync(c => c.process_instance_number == proc_ioleinfor.process_instance_number);
                    await process_InstanceServices.DelInfor_Async(shjenpiId);
                }
                else
                {
                    var lskl = await process_NodesServices.GetShowObjAsync(c => c.upper_node == liuc.lower_node);

                    //修改审批实例表
                    proc_ioleinfor.operator_number = gesss.process_nodes_role_number;
                    proc_ioleinfor.operator_name = gesss.process_nodes_role_name;
                    proc_ioleinfor.to_da_role_number = gesss.upper_node;
                    proc_ioleinfor.to_da_role = gesss.upper_node;
                    proc_ioleinfor.current_node_number = gesss.process_nodes_role_number;
                    proc_ioleinfor.current_node_name = gesss.process_nodes_role_name;
                    await process_InstanceServices.UpdInfor_Async(proc_ioleinfor);
                   
                }

                apiReturn.Code = 200;
                apiReturn.Message = "审批成功";
                process_RecordsRepository.CommitTranInfor();
                return apiReturn;
            }
            catch (Exception)
            {

                process_RecordsRepository.RollbackTranInfor();
                throw;
            }
        }
    }
}
